package me.firstapp.bbs.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import me.firstapp.bbs.base.BaseController;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonCollection;
import me.firstapp.common.module.shop.JsonProduct;
import me.firstapp.common.module.shop.JsonProductComment;
import me.firstapp.common.module.shop.JsonProductSkuRelation;
import me.firstapp.common.module.shop.TempshoppingCart;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.spi.bbs.CollectionService;
import me.firstapp.spi.shop.ProductCommentService;
import me.firstapp.spi.shop.ProductService;
import me.firstapp.spi.shop.ProductSkuRelService;

@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {

	@Autowired
	private ProductService productService;

	@Autowired
	private ProductSkuRelService productSkuRelService;

	@Autowired
	private CollectionService collectionService;

	@Autowired
	private ProductCommentService productCommentService;

	@Value("#{configProperties['bbs.url']}")
	private String bbsUrl;

	@RequestMapping(value = "/{productId}")
	public ModelAndView productDetail(@PathVariable Long productId, HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		// 商品
		SingleObject<JsonProduct> result = productService.findProductById(productId);
		JsonProduct jsonProduct = result.getObject();
		if (null == jsonProduct) {
			throw new ServiceException(StatusHouse.PRODUCT_NOT_EXIST);
		}
		modelMap.put("product", result.getObject());

		// sku
		ListObject<JsonProductSkuRelation> skuRelsResult = productSkuRelService
				.findProductSkuRelationsByProductId(productId, null);
		List<JsonProductSkuRelation> skuRelsList = skuRelsResult.getItems();
		// 将默认的属性放在第一位
		for (JsonProductSkuRelation apiJsonProductSkuRelation : skuRelsList) {
			if (apiJsonProductSkuRelation.getIsDefault() == 1) {
				skuRelsList.remove(apiJsonProductSkuRelation);
				skuRelsList.add(0, apiJsonProductSkuRelation);
				break;
			}
		}
		modelMap.put("skuRels", skuRelsList);

		// 购物车
		TempshoppingCart tempShoppingCart = getTempshoppingCart(request, response);
		modelMap.put("tempCart", tempShoppingCart);

		// 收藏
		Long memberId = super.getLoginMemberId(request, response);
		if (memberId != null) {
			SingleObject<JsonCollection> colResult = collectionService.findMemberProductCollectionRel(memberId,
					productId);
			JsonCollection collection = colResult.getObject();
			if (collection == null) {
				modelMap.put("isCollect", false);
			} else {
				modelMap.put("isCollect", true);
			}
		}

		// 评论
		ListObject<JsonProductComment> commentResult = productCommentService.findProductCommentList(productId);
		modelMap.put("commentList", commentResult.getItems());

		modelMap.put("bbsUrl", bbsUrl);

		return new ModelAndView("shop/productDetail", modelMap);
	}

	@RequestMapping(value = "/findSkuRelById.do", method = RequestMethod.POST)
	public void findSkuRelById(Long relId, HttpServletResponse response) {
		SingleObject<JsonProductSkuRelation> relResult = productSkuRelService.findProductSkuRelationById(relId);
		ResponseUtils.renderJson(response, relResult);
	}
}
